<template>
  <div class="tinymce-editor">
    <editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick"
    >
    </editor>
  </div>
</template>
<script>
import tinymce from 'tinymce/tinymce';
import Editor from '@tinymce/tinymce-vue';
import 'tinymce/themes/silver';
import 'tinymce/icons/default/icons'; // 解决了icons.js 报错Unexpected token '<'
// 编辑器插件plugins
// 更多插件参考：https://www.tiny.cloud/docs/plugins/
import 'tinymce/plugins/code'; // 插入代码
import 'tinymce/plugins/image'; // 插入上传图片插件
import 'tinymce/plugins/lists'; // 插入列表
import 'tinymce/plugins/media'; // 插入视频
import 'tinymce/plugins/paste'; // 粘贴
import 'tinymce/plugins/table'; // 插入表格
import 'tinymce/plugins/wordcount'; // 字数统计插件

import { upload } from '@/api/common/file';

export default {
  components: {
    Editor,
  },
  props: {
    statusbar: {
      type: Boolean,
      default: true,
    },
    height: {
      type: Number,
      default: 200,
    },
    value: {
      type: String,
      default: '',
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    plugins: {
      type: [String, Array],
      default: 'code image lists media table paste wordcount',
    },
    toolbar: {
      type: [String, Array],
      default:
        'undo redo | formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table paste | removeformat',
    },
  },
  data() {
    return {
      init: {
        statusbar: this.statusbar,
        relative_urls: false,
        remove_script_host: true,
        document_base_url: 'http://localhost:10680/dev-api/',
        language_url: `/tinymce/langs/zh_CN.js`,
        language: 'zh_CN',
        skin_url: `/tinymce/skins/ui/oxide`,
        content_css: `/tinymce/skins/content/default/content.css`,
        height: this.height,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        paste_data_images: true,
        /**
         * 图片上传处理(Base64格式)
         * ajax上传参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
         */
        images_upload_handler: (blobInfo, success, failure) => {
          const formData = new FormData();
          formData.append('file', blobInfo.blob(), blobInfo.filename());
          upload(formData).then((res) => {
            const { fileName } = res;
            success(process.env.VUE_APP_BASE_API + fileName);
          });
        },
      },
      myValue: this.value,
    };
  },
  mounted() {
    tinymce.init({});
  },
  methods: {
    // 添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    onClick(e) {
      this.$emit('onClick', e, tinymce);
    },
    clear() {
      this.myValue = '';
    },
  },
  watch: {
    value(newValue) {
      this.myValue = newValue;
    },
    myValue(newValue) {
      this.$emit('input', newValue);
    },
  },
};
</script>
